<?php

use FastRoute\RouteCollector;
use FastRoute\Dispatcher;

$dispatcher = FastRoute\simpleDispatcher(function(RouteCollector $r) {
    $r->addGroup('/wzh/public/index.php',function (RouteCollector $r){

        $r->addRoute('GET','/test',function (){
            echo "test";
        });
        $r->addRoute('GET', '/users', 'get_all_users_handler');
        $r->addRoute('GET','/index','Index/index');
    });
    
    // // {id} must be a number (\d+)
    // $r->addRoute('GET', '/wzh/public/index.php/user/{id:\d+}', 'get_user_handler');
    // // The /{title} suffix is optional
    // $r->addRoute('GET', '/wzh/public/index.php/articles/{id:\d+}[/{title}]', 'get_article_handler');
});

// Fetch method and URI from somewhere
$httpMethod = $_SERVER['REQUEST_METHOD'];
$uri = $_SERVER['REQUEST_URI'];

// Strip query string (?foo=bar) and decode URI
if (false !== $pos = strpos($uri, '?')) {
    $uri = substr($uri, 0, $pos);
}
$uri = rawurldecode($uri);

$routeInfo = $dispatcher->dispatch($httpMethod, $uri);
switch ($routeInfo[0]) {
    case Dispatcher::NOT_FOUND:
        // ... 404 Not Found
        echo "404";
        break;
    case Dispatcher::METHOD_NOT_ALLOWED:
        $allowedMethods = $routeInfo[1];
        // ... 405 Method Not Allowed
        echo "405";
        break;
    case Dispatcher::FOUND:
        $handler = $routeInfo[1];
        $vars = $routeInfo[2];
        // ... call $handler with $vars
        if($handler instanceof Closure){
            call_user_func_array($handler,$vars);
        }elseif(is_string($handler)){
            Processing($handler);
            // new $handler;
            
        }
        break;
}

function Processing($handler){

    $parts = explode("/",$handler,3);
    if(count($parts)<2){
        echo "FALSE";
        return ;
    }

    $controller = new $parts[0];
    // $test = 'app\\controller\\' . $parts[0];
    // $controller = new $test;
    $test = $controller->{$parts[1]}();

    return ;
}